package com.jcw.kqxt.service.impl;

import com.jcw.kqxt.constants.Constant;
import com.jcw.kqxt.domain.Dept;
import com.jcw.kqxt.mapper.DeptMapper;
import com.jcw.kqxt.result.JSONResult;
import com.jcw.kqxt.service.IDeptService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qinhui
 * @since 2022-02-25
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public JSONResult deptTree() {
        //先查询所有部门数据
        List<Dept> allCourseTypes = super.selectList(null);
        //装一级部门
        List<Dept> firstDepts = new ArrayList<>();
        for (Dept dept : allCourseTypes) {
            if (dept.getpId() == null || dept.getpId().longValue() == 0) {
                //查找一级部门
                firstDepts.add(dept);
            }
            else{
                for(Dept deptParen : allCourseTypes){
                    if (dept.getpId().longValue()==deptParen.getId().longValue()){
                        deptParen.getChildren().add(dept);
                        break;
                    }
                }

            }
        }

        return JSONResult.success(firstDepts);
    }

    @Override
    public JSONResult deptDisable(Long id) {
        Dept dept = super.selectById(id);
        dept.setIsDelete(Constant.DEPT_IS_DELETE);
        super.updateById(dept);
        return JSONResult.success();
    }
}
